<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>jQuery treeTable Plugin Documentation</title>
  <link href="master.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="jquery.js"></script>

  <!-- BEGIN Plugin Code -->
  
  <link href="jquery.treeTable.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="jquery.treeTable.js"></script>
  <script type="text/javascript">
  
  $(document).ready(function() {
    // TODO Fix issue with multiple treeTables on one page, each with different options
    // Moving the #example3 treeeTable call down will break other treeTables that are expandable...
    $("#example3").treeTable({
      expandable: false
    });
    
    $(".example").treeTable();

    // Drag & Drop Example Code
    $("#dnd-example .file, #dnd-example .folder").draggable({
      helper: "clone",
      opacity: .75,
      refreshPositions: true, // Performance?
      revert: "invalid",
      revertDuration: 300,
      scroll: true
    });
    
    $("#dnd-example .folder").each(function() {
      $($(this).parents("tr")[0]).droppable({
        accept: ".file, .folder",
        drop: function(e, ui) { 
          $($(ui.draggable).parents("tr")[0]).appendBranchTo(this);
        },
        hoverClass: "accept",
        over: function(e, ui) {
          if(this.id != $(ui.draggable.parents("tr")[0]).id && !$(this).is(".expanded")) {
            $(this).expand();
          }
        }
      });
    });
    
    // Make visible that a row is clicked
    $("table#dnd-example tbody tr").mousedown(function() {
      $("tr.selected").removeClass("selected"); // Deselect currently selected rows
      $(this).addClass("selected");
    });
    
    // Make sure row is selected when span is clicked
    $("table#dnd-example tbody tr span").mousedown(function() {
      $($(this).parents("tr")[0]).trigger("mousedown");
    });
  });
  
  </script>

  <!-- END Plugin Code -->
</head>
<body>

<h1>jQuery treeTable Plugin Documentation</h1>

<table class="example">
  <caption>Simple treeTable Example</caption>
  <thead>
    <tr>
      <th>TreeColumn</th>
      <th>Additional data</th>
    </tr>
  </thead>
  <tbody>
    <tr id="ex0-node-1">
      <td>Node 1: Click on the icon in front of me to expand this branch.</td>
      <td>I live in the second column.</td>
    </tr>
    <tr id="ex0-node-1-1" class="child-of-ex0-node-1">
      <td>Node 1.1: Look, I am a table row <em>and</em> I am part of a tree!</td>
      <td>Interesting.</td>
    </tr>
    <tr id="ex0-node-1-1-1" class="child-of-ex0-node-1-1">
      <td>Node 1.1.1: I am part of the tree too!</td>
      <td>That"s it!</td>
    </tr>
  </tbody>
</table>

<p>See the <a href="#examples">Examples</a> chapter for more examples. To download releases of this plugin, visit <a href="http://plugins.jquery.com/project/treeTable">treeTable"s project page</a>. The source code of this plugin and this documentation is available on GitHub: <a href="http://github.com/ludo/jquery-plugins/tree/master/treeTable">http://github.com/ludo/jquery-plugins/tree/master/treeTable</a>.</p>

<h2>Table of Contents</h2>

<ol>
  <li><a href="#introduction">Introduction</a></li>
  <li><a href="#installation">Installation</a></li>
  <li><a href="#usage">Usage</a>
    <ol>
      <li><a href="#head">Include the plugin in your html document</a></li>
      <li><a href="#tree">Representing your tree in a table</a></li>
      <li><a href="#settings">Configuration</a></li>
    </ol>
  </li>
  <li><a href="#examples">Examples</a>
    <ol>
      <li><a href="#example-2">A complex tree</a></li>
      <li><a href="#example-3">A tree that is not collapsable</a></li>
      <li><a href="#example-1">Dragging and dropping example</a></li>
      <li><a href="performance.html">A large, complex table with drag and drop</a></li>
    </ol>
  </li>
</ol>

<h2>1. <a name="introduction">Introduction</a></h2>
<p>treeTable is a plugin for <a href="http://jquery.com">jQuery</a>, the "Write Less, Do More, JavaScript Library". With this plugin you can display a tree in a table, i.e. a directory structure or a nested list. Why not use a list, you say? Because lists are great for displaying a tree, and tables are not. Oh wait, but this plugin uses tables, doesn"t it? Yes. Why do I use a table to display a list? Because I need multiple columns to display additional data besides the tree.</p>

<p>This plugin is released under the MIT license by <a href="http://blog.cubicphuse.nl">Ludo van den Boom</a>.</p>

<h4>Unobtrusiveness</h4>
<p>I wanted this plugin to be as unobtrusive as possible. Being "unobtrusive" is very cool nowadays, so that was an important requirement. But it is cool for a reason: it keeps your html documents clean and it allows my code to degrade nicely when JavaScript is not available.</p>

<p>Unfortunately, the treeTable plugin requires that you add <tt>class</tt> and <tt>id</tt> attributes to every row that is part of the tree. It would have been great if this weren"t necessary, because it doesn"t look pretty in your html, but the plugin needs to know what your tree looks like. Otherwise, it would have to guess the structure of the tree and it wouldn"t be very successful in doing that. See the <a href="#usage">Usage</a> chapter for more information on how to describe a tree.</p>

<h4>Features</h4>
<ul>
  <li>It can display a tree of data in a table column.</li>
  <li>It does this as unobtrusively as possible.</li>
  <li>It allows branches to be collapsed and expanded (think of how a directory structure works in most file explorers).</li>
  <li>It allows unlimited tree depth.</li>
  <li>It uses the lightweight <a href="http://jquery.com">jQuery</a> JavaScript libray.</li>
</ul>

<p>NOTE: This plugin was originally released under the name ActsAsTreeTable, but has been renamed to treeTable with version 2.0.</p>

<h2>2. <a name="installation">Installation</a></h2>
<p>Installing this plugin is straight-forward. You will have to copy several files and, if necessary, adjust some paths so that every file is available to the plugin.</p>

<ol>
  <li>Add <a href="http://jquery.com">jQuery</a> to your project. See their website for instructions on doing this. You need at least version 1.2.6.</li>
  <li>Add <a href="../src/javascripts/jquery.treeTable.js"><tt>src/javascripts/jquery.treeTable.js</tt></a> (or the <a href="../src/javascripts/jquery.treeTable.min.js">minified</a> version) to your project.</li>
  <li>Add the stylesheet <a href="../src/stylesheets/jquery.treeTable.css"><tt>src/stylesheets/jquery.treeTable.css</tt></a> to your project.</li>
  <li>Copy the images in <a href="../src/images"><tt>src/images</tt></a> to your project.</li>
  <li>Adjust the paths to <tt>background-image</tt>s in the stylesheet <tt>jquery.treeTable.css</tt> to point to the image files that you have just copied.</li>
</ol>

<p>That"s it! You are now ready to start <a href="#usage">using</a> the plugin in your project.</p>

<h2>3. <a name="usage">Usage</a></h2>
<p><b>Note:</b> This chapter assumes that you have already installed jQuery as described on their website.</p>

<h3>3.1 <a name="head">Include the plugin in your html document</a></h3>
<p>Paste the following code between the <tt>head</tt> tags in your html document, <b>underneath</b> the part where you include jQuery. Change the <b style="color: #f00;">red</b> parts to reflect your situation.</p>

<pre class="listing">
&lt;link href="<b>path/to/</b>jquery.treeTable.css" rel="stylesheet" type="text/css" /&gt;
&lt;script type="text/javascript" src="<b>path/to/</b>jquery.treeTable.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
  
$(document).ready(function()  {
  $("<b>#your_table_id</b>").treeTable();
});
  
&lt;/script&gt;
</pre>

<h3>3.2 <a name="tree">Representing your tree in a table</a></h3>
<p>When you pasted the above code and adjusted it to reflect your situation, you enabled the <em>possibility</em> of displaying a tree in your table. To make the tree actually display as a tree you have to add <tt>id</tt> and <tt>class</tt> attributes to your table rows (<tt>tr</tt>).</p>

<h4>How to do this?</h4>
<p>First, you should add a <em>unique</em> <tt>id</tt> to each of the rows in your table, for example "<tt>node-<em>x</em></tt>" where <em>x</em> is a number. Then you add a <tt>class</tt> attribute to each child of a node, give this class a name of "<tt>child-of-<em>node-x</em></tt>". The <em>node-x</em> part should be the same as the <tt>id</tt> of its parent. Do you still follow me? Let me show you an example of a very simple tree: a single parent with a single child. For more examples you should view the source code of this page, where you find several tables for the examples in the <a href="#examples">Examples</a> chapter.</p>

<pre class="listing">
&lt;table id="tree"&gt;
  &lt;tr id="node-1"&gt;
    &lt;td&gt;Parent&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr id="node-2" class="child-of-node-1"&gt;
    &lt;td&gt;Child&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr id="node-3" class="child-of-node-2"&gt;
    &lt;td&gt;Child&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
</pre>

<p>Please note that the plugin expects the rows in the HTML table to be in the same order in which they should be displayed in the tree. For example, suppose you have three nodes: <strong style="color: red;">A</strong>, <strong style="color: navy;">B</strong> (child of node <span style="color: red">A</span>) and <strong style="color: green;">C</strong> (child of node <span style="color: navy">B</span>). If you create rows for these nodes in your HTML table in the following order <span style="color: red">A</span> - <span style="color: green">C</span> - <span style="color: navy">B</span>, then the tree will not display correctly. You have to make sure that the rows are in the order <span style="color: red">A</span> - <span style="color: navy">B</span> - <span style="color: green">C</span>.</p>

<h3>3.3 <a name="settings">Configuration</a></h3>
<p>There are several settings that let you adjust the behavior of the plugin. Each of these settings is described in this section. See <a href="#example-3">Example 3</a> for an example of how to change these settings.</p>

<table>
  <thead>
    <tr>
      <th>Setting</th>
      <th>Type</th>
      <th>Default</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><tt>childPrefix</tt></td>
      <td><tt>string</tt></td>
      <td><tt>&quot;child-of-&quot;</tt></td>
      <td>Customize the prefix used for node classes.</td>
    </tr>
    <tr>
      <td><tt>clickableNodeNames</tt></td>
      <td><tt>bool</tt></td>
      <td><tt>false</tt></td>
      <td>Set to true to expand branches not only when expander icon is clicked but also when node name is clicked.</td>
    </tr>
    <tr>
      <td><tt>expandable</tt></td>
      <td><tt>bool</tt></td>
      <td><tt>true</tt></td>
      <td>Should the tree be expandable? An expandable tree contains buttons to make each branch with children collapsable/expandable.</td>
    </tr>
    <tr>
      <td><tt>indent</tt></td>
      <td><tt>int</tt></td>
      <td><tt>19</tt></td>
      <td>The number of pixels that each branch should be indented with.</td>
    </tr>
    <tr>
      <td><tt>initialState</tt></td>
      <td><tt>string</tt></td>
      <td><tt>&quot;expanded&quot;</tt></td>
      <td>Possible values: <tt>&quot;expanded&quot;</tt> or <tt>&quot;collapsed&quot;</tt>.</td>
    </tr>
    <tr>
      <td><tt>treeColumn</tt></td>
      <td><tt>int</tt></td>
      <td><tt>0</tt></td>
      <td>The number of the column in the table that should be displayed as a tree.</td>
    </tr>
  </tbody>
</table>

<h2>4. <a name="examples">Examples</a></h2>
<p>The examples in this chapter all use the treeTable plugin to display a tree in a table, with collapsable branches. View the source code of this file to see how it is done and read the <a href="#usage">Usage</a> chapter for further details.</p>

<h3>4.1 <a name="example-2">A complex tree</a></h3>

<table class="example">
  <caption>Example 1: A complex tree.</caption>
  <tr id="ex2-node-1">
    <td>Node 1</td>
  </tr>
  <tr id="ex2-node-1-1" class="child-of-ex2-node-1">
    <td>Node 1.1</td>
  </tr>
  <tr id="ex2-node-1-2" class="child-of-ex2-node-1">
    <td>Node 1.2</td>
  </tr>
  <tr id="ex2-node-1-3" class="child-of-ex2-node-1">
    <td>Node 1.3</td>
  </tr>
  <tr id="ex2-node-2">
    <td>Node 2</td>
  </tr>
  <tr id="ex2-node-2-1" class="child-of-ex2-node-2">
    <td>Node 2.1</td>
  </tr>
  <tr id="ex2-node-2-1-1" class="child-of-ex2-node-2-1">
    <td>Node 2.1.1</td>
  </tr>
  <tr id="ex2-node-2-2" class="child-of-ex2-node-2">
    <td>Node 2.2</td>
  </tr>
  <tr id="ex2-node-2-2-1" class="child-of-ex2-node-2-2">
    <td>Node 2.2.1</td>
  </tr>
  <tr id="ex2-node-2-2-1-1" class="child-of-ex2-node-2-2-1">
    <td>Node 2.2.1.1</td>
  </tr>
  <tr id="ex2-node-2-2-2" class="child-of-ex2-node-2-2">
    <td>Node 2.2.2</td>
  </tr>
</table>

<h3>4.2 <a name="example-3">A tree that is not collapsable</a></h3>

<pre class="listing">
$("#example3").treeTable({
  expandable: false
});
</pre>  

<table id="example3">
  <caption>Example 2: A tree that is not collapsable.</caption>
  <thead>
    <tr>
      <th>Tree column</th>
      <th>Column 2</th>
    </tr>
  </thead>
  <tbody>
    <tr id="ex3-node-1">
      <td>Node 1</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-1-1" class="child-of-ex3-node-1">
      <td>Node 1.1</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-1-2" class="child-of-ex3-node-1">
      <td>Node 1.2</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-1-3" class="child-of-ex3-node-1">
      <td>Node 1.3</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-2">
      <td>Node 2</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-2-1" class="child-of-ex3-node-2">
      <td>Node 2.1</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-2-1-1" class="child-of-ex3-node-2-1">
      <td>Node 2.1.1</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-2-2" class="child-of-ex3-node-2">
      <td>Node 2.2</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-2-2-1" class="child-of-ex3-node-2-2">
      <td>Node 2.2.1</td>
      <td>Second column</td>
    </tr>
    <tr id="ex3-node-2-2-2" class="child-of-ex3-node-2-2">
      <td>Node 2.2.2</td>
      <td>Second column</td>
    </tr>
  </tbody>
</table>

<h3>4.3 <a name="example-1">Draging and dropping example</a></h3>

<p>This example uses the <a href="http://ui.jquery.com" title="jQuery UI Website">jQuery UI</a> components Draggable and Droppable to create a tree that can be manipulated by dragging and dropping of the nodes. You can drag a node by clicking on it"s title and drag it to a different position. This behavior requires a bit more coding than the simple trees above, but it is still pretty straight-forward. The code for this example is listed below. The most interesting line in this code, from the plugin"s point of view is <tt>$($(ui.draggable).parents("tr")).appendBranchTo(this);</tt>. Here the <tt>appendBranchTo</tt> function is called to move the selected branch to a new location.</p>
<table class="example" id="dnd-example">
  <caption>Example 3: Dragging and dropping example.</caption>
  <thead>
    <tr>
      <th>Title</th>
      <th>Size</th>
      <th>Kind</th>
	  <th>Title</th>
      <th>Size</th>
      <th>Kind</th>
	  <th>Title</th>
    </tr>
  </thead>
  <tbody>
<tr class='trlinebg' id='node-26'><td><span class="folder">about us</span></td><td>&nbsp;</td><td><input type='checkbox' value='26' class='chkbox' name='select' /></td><td><input type='checkbox' value='26' class='chkbox' name='insert' /></td><td><input type='checkbox' value='26' class='chkbox' name='update' /></td><td><input type='checkbox' value='26' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-26' id='node-35'><td><span class="file">Company </span></td><td>&nbsp;</td><td><input type='checkbox' value='35' class='chkbox' name='select' /></td><td><input type='checkbox' value='35' class='chkbox' name='insert' /></td><td><input type='checkbox' value='35' class='chkbox' name='update' /></td><td><input type='checkbox' value='35' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-26' id='node-36'><td><span class="file">Culture</span></td><td>&nbsp;</td><td><input type='checkbox' value='36' class='chkbox' name='select' /></td><td><input type='checkbox' value='36' class='chkbox' name='insert' /></td><td><input type='checkbox' value='36' class='chkbox' name='update' /></td><td><input type='checkbox' value='36' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-26' id='node-37'><td><span class="file">Qualification </span></td><td>&nbsp;</td><td><input type='checkbox' value='37' class='chkbox' name='select' /></td><td><input type='checkbox' value='37' class='chkbox' name='insert' /></td><td><input type='checkbox' value='37' class='chkbox' name='update' /></td><td><input type='checkbox' value='37' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg' id='node-27'><td><span class="file">Contact us </span></td><td>&nbsp;</td><td><input type='checkbox' value='27' class='chkbox' name='select' /></td><td><input type='checkbox' value='27' class='chkbox' name='insert' /></td><td><input type='checkbox' value='27' class='chkbox' name='update' /></td><td><input type='checkbox' value='27' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg' id='node-28'><td><span class="folder">Products</span></td><td>&nbsp;</td><td><input type='checkbox' value='28' class='chkbox' name='select' /></td><td><input type='checkbox' value='28' class='chkbox' name='insert' /></td><td><input type='checkbox' value='28' class='chkbox' name='update' /></td><td><input type='checkbox' value='28' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-28' id='node-38'><td><span class="folder">asdfsadf</span></td><td>&nbsp;</td><td><input type='checkbox' value='38' class='chkbox' name='select' /></td><td><input type='checkbox' value='38' class='chkbox' name='insert' /></td><td><input type='checkbox' value='38' class='chkbox' name='update' /></td><td><input type='checkbox' value='38' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-38' id='node-39'><td><span class="file">asdfasdf</span></td><td>&nbsp;</td><td><input type='checkbox' value='39' class='chkbox' name='select' /></td><td><input type='checkbox' value='39' class='chkbox' name='insert' /></td><td><input type='checkbox' value='39' class='chkbox' name='update' /></td><td><input type='checkbox' value='39' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-28' id='node-29'><td><span class="folder">Rigid Paper Box  </span></td><td>&nbsp;</td><td><input type='checkbox' value='29' class='chkbox' name='select' /></td><td><input type='checkbox' value='29' class='chkbox' name='insert' /></td><td><input type='checkbox' value='29' class='chkbox' name='update' /></td><td><input type='checkbox' value='29' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>

<tr class='trlinebg child-of-node-29' id='node-30'><td><span class="file">Gift & Jewellery Box</span></td><td>&nbsp;</td><td><input type='checkbox' value='30' class='chkbox' name='select' /></td><td><input type='checkbox' value='30' class='chkbox' name='insert' /></td><td><input type='checkbox' value='30' class='chkbox' name='update' /></td><td><input type='checkbox' value='30' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-29' id='node-31'><td><span class="file">Display Box</span></td><td>&nbsp;</td><td><input type='checkbox' value='31' class='chkbox' name='select' /></td><td><input type='checkbox' value='31' class='chkbox' name='insert' /></td><td><input type='checkbox' value='31' class='chkbox' name='update' /></td><td><input type='checkbox' value='31' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-29' id='node-32'><td><span class="file">Food Packaging Box</span></td><td>&nbsp;</td><td><input type='checkbox' value='32' class='chkbox' name='select' /></td><td><input type='checkbox' value='32' class='chkbox' name='insert' /></td><td><input type='checkbox' value='32' class='chkbox' name='update' /></td><td><input type='checkbox' value='32' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg child-of-node-29' id='node-33'><td><span class="file">Other Packaging Box</span></td><td>&nbsp;</td><td><input type='checkbox' value='33' class='chkbox' name='select' /></td><td><input type='checkbox' value='33' class='chkbox' name='insert' /></td><td><input type='checkbox' value='33' class='chkbox' name='update' /></td><td><input type='checkbox' value='33' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>
<tr class='trlinebg' id='node-34'><td><span class="file">Guestbook</span></td><td>&nbsp;</td><td><input type='checkbox' value='34' class='chkbox' name='select' /></td><td><input type='checkbox' value='34' class='chkbox' name='insert' /></td><td><input type='checkbox' value='34' class='chkbox' name='update' /></td><td><input type='checkbox' value='34' class='chkbox' name='delete' /></td><td>&nbsp;</td></tr>


  </tbody>
</table>

<pre class="listing">
/* NOTE: Do not forget to download the jQuery UI Draggable and Droppable
 * components if you want to enable dragging and dropping behavior!
 */
  
// Configure draggable nodes
$("#dnd-example .file, #dnd-example .folder").draggable({
  helper: "clone",
  opacity: .75,
  refreshPositions: true, // Performance?
  revert: "invalid",
  revertDuration: 300,
  scroll: true
});

// Configure droppable rows
$("#dnd-example .folder").each(function() {
  $(this).parents("tr").droppable({
    accept: ".file, .folder",
    drop: function(e, ui) { 
      // Call jQuery treeTable plugin to move the branch
      $($(ui.draggable).parents("tr")).appendBranchTo(this);
    },
    hoverClass: "accept",
    over: function(e, ui) {
      // Make the droppable branch expand when a draggable node is moved over it.
      if(this.id != $(ui.draggable.parents("tr")[0]).id && !$(this).is(".expanded")) {
        $(this).expand();
      }
    }
  });
});

// Make visible that a row is clicked
$("table#dnd-example tbody tr").mousedown(function() {
  $("tr.selected").removeClass("selected"); // Deselect currently selected rows
  $(this).addClass("selected");
});

// Make sure row is selected when span is clicked
$("table#dnd-example tbody tr span").mousedown(function() {
  $($(this).parents("tr")[0]).trigger("mousedown");
});
</pre>

</body>
</html>